package com.sinosoft.discovery.configclient;

import com.sinosoft.configServer.ConfigServerProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties;
import org.springframework.cloud.alibaba.nacos.discovery.NacosDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;

/**
 * @author loujiang
 * @date 2020/12/26 1:52 上午
 * @NOTE:
 */
@Configuration
@EnableConfigurationProperties
@ConditionalOnClass({ NacosDiscoveryProperties.class, NacosDiscoveryClient.class,ConfigServerProperties.class })
public class NacosConfigServerAutoConfiguration {



    /**
     * The metadata key for the path for config server.
     */
    public static final String CONFIG_PATH_KEY = "configPath";

    @Autowired(required = false)
    private NacosDiscoveryProperties properties;

    @Autowired(required = false)
    private ConfigServerProperties server;


    @PostConstruct
    public void init() {
        if (this.properties == null || this.server == null) {
            return;
        }
        String prefix = this.server.getPrefix();
        if (StringUtils.hasText(prefix)) {
            this.properties.getMetadata().put(CONFIG_PATH_KEY, prefix);
        }
    }
}
